package com.example.administrator.about_huanxin.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;

import com.example.administrator.about_huanxin.CheckPermissionUtils;
import com.example.administrator.about_huanxin.R;
import com.example.administrator.about_huanxin.ShareprefenceUtils;

public class SplashActivity extends AppCompatActivity {

    private Handler h = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String result = ShareprefenceUtils.getResult(SplashActivity.this,"isAuto","");
            if(TextUtils.isEmpty(result)){
                startActivity(new Intent(SplashActivity.this,RegisiterActivity.class));
            }else{
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
            }
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);

        String[] permissions = CheckPermissionUtils.checkPermission(this);
        if(permissions.length == 0){
            //权限都申请了
            h.sendEmptyMessageDelayed(100,2000);
        }else{
            //申请权限
            ActivityCompat.requestPermissions(this,permissions,100);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 100){
            for (int grantResult : grantResults) {
                if(grantResult == PackageManager.PERMISSION_DENIED){ //用户手动不申请权限
                    finish();
                    return;
                }
            }
            h.sendEmptyMessageDelayed(100,500);
        }
    }
}
